# -------------------------------------------------------------------------------
# Description:
# Reference:
# Name:   relaxtionTransfer
# Author: wujun
# Date:   2021-12-17
# -------------------------------------------------------------------------------
import json
import os

def findAllAtrributes(filePath):
    filelist = os.listdir(filePath)
    attributesList = []
    for file in filelist:
        filePath = r'D:\protegeAuto_tool\JsonCenter\document\\' + str(file)
        # 读取json文件内容,返回字典格式
        with open(filePath,'r',encoding='utf8')as fp:
            json_data = json.load(fp)
            oneList = list(set(list(json_data['attributes'].keys())))
            for i in oneList:
                if(i in attributesList):
                    pass
                else:
                    attributesList.append(i)
        fp.close()
    return attributesList

def attrribute2relaxtion(filePath,attrribute,relaxtion):
    filelist = os.listdir(filePath)
    for file in filelist:
        try:
            filePath = r'D:\protegeAuto_tool\JsonCenter\document\\' + str(file)
            # 读取json文件内容,返回字典格式
            with open(filePath, 'r', encoding='utf-8')as fp:
                json_data = json.load(fp)
                attributesDic = json_data['attributes']
                if(attrribute in list(attributesDic.keys())):
                    if("，" in attributesDic[attrribute]):
                        json_data['relation'][relaxtion] = attributesDic[attrribute].split('，')
                    elif("、" in attributesDic[attrribute]):
                        json_data['relation'][relaxtion] = attributesDic[attrribute].split('、')
                    elif ("；" in attributesDic[attrribute]):
                        json_data['relation'][relaxtion] = attributesDic[attrribute].split('；')
                    elif (" " in attributesDic[attrribute]):
                        json_data['relation'][relaxtion] = attributesDic[attrribute].split(' ')
                    else:
                        json_data['relation'][relaxtion] = [attributesDic[attrribute]]

                print(json_data)
            fp.close()
            with open(filePath, 'w', encoding='utf-8') as json_file:
                json_str = json.dumps(json_data, indent=4, ensure_ascii=False)
                json_file.write(json_str)
            print("{}已update".format(filePath))
        except:
            print("{}无法解析".format(file))
            pass

allAtrributes = findAllAtrributes(r'D:\protegeAuto_tool\JsonCenter\document')
print("共查到{}个属性，具体为{}".format(len(allAtrributes),allAtrributes))

filePath = r'D:\protegeAuto_tool\JsonCenter\document'
attrribute2relaxtion(filePath,'制定者',"相关人物")
attrribute2relaxtion(filePath,'通过者',"相关组织机构")
attrribute2relaxtion(filePath,'签署国',"相关组织机构")
attrribute2relaxtion(filePath,'签署地点',"签订地点")
attrribute2relaxtion(filePath,'签署时间',"签订时间")
attrribute2relaxtion(filePath,'签约地点',"签订地点")
attrribute2relaxtion(filePath,'签约双方',"相关组织机构")
attrribute2relaxtion(filePath,'别名',"别名")
attrribute2relaxtion(filePath,'签约时间',"签订时间")
attrribute2relaxtion(filePath,'英国代表',"相关人物")
attrribute2relaxtion(filePath,'中国代表',"相关人物")
attrribute2relaxtion(filePath,'签订时间 ',"签订时间")
attrribute2relaxtion(filePath,'地点',"签订地点")
attrribute2relaxtion(filePath,'签订时间',"签订时间")
attrribute2relaxtion(filePath,'发生时间 ',"签订时间")
attrribute2relaxtion(filePath,'相关人物',"相关人物")
attrribute2relaxtion(filePath,'签订双方',"相关组织机构")
attrribute2relaxtion(filePath,'签订地点',"签订地点")
attrribute2relaxtion(filePath,'双方代表',"相关人物")
attrribute2relaxtion(filePath,'签约国',"相关组织机构")
attrribute2relaxtion(filePath,'双方',"相关组织机构")
attrribute2relaxtion(filePath,'中方代表',"相关人物")
attrribute2relaxtion(filePath,'时间',"签订时间")
attrribute2relaxtion(filePath,'签约方',"相关组织机构")
attrribute2relaxtion(filePath,'日本代表',"相关人物")